package com.frontend.screens;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.data.documents.PatientInformation;
import com.frontend.ConnectionException;
import com.frontend.DoctorsOfficeClient;
import com.users.User;

public class PatientInformationScreen extends Screen {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -402660985755460485L;
	/**
	 * 
	 */
	private JLabel nameLabel, addressLabel,phoneLabel, emailLabel, genderLabel, ageLabel, pharmacyLabel,
					insuranceLabel, allergiesLabel, medicalHistoryLabel, message;
	private JTextField name, address, phone, email, gender, age, pharmacy, insurance, allergies, medicalHistory;
	private JPanel namePanel, addressPanel, phonePanel, emailPanel, genderPanel, agePanel, 
					pharmacyPanel, insurancePanel, allergiesPanel, medicalHistoryPanel;
	private JPanel b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11;
	private JButton submit, back;
	private boolean block;
	private javax.swing.Timer timer;
	private int cycles;
	private PatientInformation pi;
	private User u;
	private Screen prevScreen;
	

	/**
	 * Makes the appointment page. If a brand new appointment is being created, there is nothing in it. 
	 * If it is being edited, the values from the appointment are filled into the text boxes
	 * @param window
	 * @param pi
	 */
	public PatientInformationScreen(DoctorsOfficeClient window, PatientInformation pi, User u, Screen prevScreen){
		super(window);
		this.prevScreen = prevScreen;
		this.pi = pi;
		this.u = u;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		timer = new javax.swing.Timer(50, new AnimationListener());
		block = true;
		InformationListener il = new InformationListener();
		
		b1 = new JPanel();
		b1.setOpaque(false);

		message = new JLabel("Patient Information");
		message.setAlignmentX(CENTER_ALIGNMENT);

		b2 = new JPanel();
		b2.setOpaque(false);
		
		namePanel = new JPanel();
		namePanel.setOpaque(false);
		namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.X_AXIS));
		nameLabel = new JLabel("Name: ");
		name= new JTextField();
		name.addActionListener(il);
		namePanel.add(nameLabel);
		namePanel.add(name);
		
		
		b3 = new JPanel();
		b3.setOpaque(false);
		
		addressPanel = new JPanel();
		addressPanel.setOpaque(false);
		addressPanel.setLayout(new BoxLayout(addressPanel, BoxLayout.X_AXIS));
		addressLabel = new JLabel("Address: ");
		address = new JTextField();
		address.addActionListener(il);
		addressPanel.add(addressLabel);
		addressPanel.add(address);
		
		b4 = new JPanel();
		b4.setOpaque(false);
		
		phonePanel = new JPanel();
		phonePanel.setOpaque(false);
		phonePanel.setLayout(new BoxLayout(phonePanel, BoxLayout.X_AXIS));
		phoneLabel = new JLabel("Phone Number: ");
		phone = new JTextField();
		phone.addActionListener(il);
		phonePanel.add(phoneLabel);
		phonePanel.add(phone);
			
		b5 = new JPanel();
		b5.setOpaque(false);
		
		emailPanel = new JPanel();
		emailPanel.setOpaque(false);
		emailPanel.setLayout(new BoxLayout(emailPanel, BoxLayout.X_AXIS));
		emailLabel = new JLabel("Email: ");
		email = new JTextField();
		email.addActionListener(il);
		emailPanel.add(emailLabel);
		emailPanel.add(email);
		
		b6 = new JPanel();
		b6.setOpaque(false);
		
		genderPanel = new JPanel();
		genderPanel.setOpaque(false);
		genderPanel.setLayout(new BoxLayout(genderPanel, BoxLayout.X_AXIS));
		genderLabel = new JLabel("Gender: ");
		gender = new JTextField();
		gender.addActionListener(il);
		genderPanel.add(genderLabel);
		genderPanel.add(gender);
		
		b7 = new JPanel();
		b7.setOpaque(false);
		
		agePanel = new JPanel();
		agePanel.setOpaque(false);
		agePanel.setLayout(new BoxLayout(agePanel, BoxLayout.X_AXIS));
		ageLabel = new JLabel("Age: ");
		age = new JTextField();
		age.addActionListener(il);
		agePanel.add(ageLabel);
		agePanel.add(age);
		
		b8 = new JPanel();
		b8.setOpaque(false);
		
		pharmacyPanel = new JPanel();
		pharmacyPanel.setOpaque(false);
		pharmacyPanel.setLayout(new BoxLayout(pharmacyPanel, BoxLayout.X_AXIS));
		pharmacyLabel = new JLabel("Pharmacy Information: ");
		pharmacy= new JTextField();
		pharmacy.addActionListener(il);
		pharmacyPanel.add(pharmacyLabel);
		pharmacyPanel.add(pharmacy);
		
		b9 = new JPanel();
		b9.setOpaque(false);
		
		insurancePanel = new JPanel();
		insurancePanel.setOpaque(false);
		insurancePanel.setLayout(new BoxLayout(insurancePanel, BoxLayout.X_AXIS));
		insuranceLabel = new JLabel("Insurance: ");
		insurance = new JTextField();
		insurance.addActionListener(il);
		insurancePanel.add(insuranceLabel);
		insurancePanel.add(insurance);
		
		b10 = new JPanel();
		b10.setOpaque(false);
		
		allergiesPanel = new JPanel();
		allergiesPanel.setOpaque(false);
		allergiesPanel.setLayout(new BoxLayout(allergiesPanel, BoxLayout.X_AXIS));
		allergiesLabel = new JLabel("Allergies: ");
		allergies= new JTextField();
		allergies.addActionListener(il);
		allergiesPanel.add(allergiesLabel);
		allergiesPanel.add(allergies);
		
		b11 = new JPanel();
		b11.setOpaque(false);
		
		medicalHistoryPanel = new JPanel();
		medicalHistoryPanel.setOpaque(false);
		medicalHistoryPanel.setLayout(new BoxLayout(medicalHistoryPanel, BoxLayout.X_AXIS));
		medicalHistoryLabel = new JLabel("Medical History: ");
		medicalHistory = new JTextField();
		medicalHistory.addActionListener(il);
		medicalHistoryPanel.add(medicalHistoryLabel);
		medicalHistoryPanel.add(medicalHistory);
		
		submit = new JButton("Submit");
		back = new JButton("Back");
		submit.addActionListener(il);
		back.addActionListener(il);
		submit.setOpaque(false);
		back.setOpaque(false);
		
		
		name.setText(pi.getName());
		address.setText(pi.getAddress());
		phone.setText(pi.getPhone());
		email.setText(pi.getEmail());
		gender.setText(pi.getGender());
		age.setText(""+pi.getAge());
		pharmacy.setText(pi.getPharmacy());
		insurance.setText(pi.getInsurance());
		allergies.setText(pi.getAllergies());
		//medicalHistory.setText(pi.getMedicalHistory());
		
		
		add(b1);
		add(message);
		add(b2);
		add(namePanel);
		add(b3);
		add(addressPanel);
		add(b4);
		add(phonePanel);
		add(b5);
		add(emailPanel);
		add(b6);
		add(genderPanel);
		add(b7);
		add(agePanel);
		add(b8);
		add(pharmacyPanel);
		add(b9);
		add(insurancePanel);
		add(b10);
		add(allergiesPanel);
		add(b11);
		add(submit);
		add(back);
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (getHeight() != tempheight || getWidth() != tempwidth) {
			message.setFont(new Font("Arial", Font.BOLD, getHeight() / 15));
			nameLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			name.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			name.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			addressLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			address.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			address.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			phoneLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			phone.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			phone.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			emailLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			email.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			email.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			genderLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			gender.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			gender.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			ageLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			age.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			age.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			pharmacyLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			pharmacy.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			pharmacy.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			insuranceLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			insurance.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			insurance.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			allergiesLabel.setFont(new Font("Arial", Font.PLAIN,
					getHeight() / 20));
			allergies.setMaximumSize(new Dimension(getWidth() / 2,
					getHeight() / 15));
			allergies.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			submit.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 15));
			submit.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));
			back.setMaximumSize(new Dimension(getWidth() / 5,
					getHeight() / 15));
			back.setFont(new Font("Arial", Font.PLAIN, getHeight() / 20));

			tempheight = getHeight();
			tempwidth = getWidth();
		}
	}

	
	private class InformationListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if (e.getSource() == back) {
				window.changeScreen(prevScreen);
				prevScreen.repaint();
				if (prevScreen instanceof PatientOverviewScreen) {
					((PatientOverviewScreen) prevScreen).search();
				}

			} 
			else{
				pi.setName(name.getText());
				pi.setAddress(address.getText());
				pi.setPhone(phone.getText());
				pi.setEmail(email.getText());
				pi.setGender(gender.getText());
				pi.setAge(Integer.parseInt(age.getText()));
				pi.setPharmacy(pharmacy.getText());
				pi.setInsurance(insurance.getText());
				pi.setAllergies(allergies.getText());
				//pi.setMedicalHistory(phone.getText());
				
				block = false;
				
				if(pi.getName().equals("")){
					block = true;
					showError("Name field cannot be blank");
				}
				
				if(!block){
					try {
						window.getNet().updateDocument(pi, u.getUsername());
						block = true;
						showError("Patient Information Submitted!");
					} catch (ConnectionException e2) {
						e2.printStackTrace();
					}
				}
			}
		}
	}

	private void showError(String line){
		timer.stop();
		block = true;
		cycles = 0;
		message.setText(line);
		message.setForeground(Color.red);
		timer.start();
	}

	private class AnimationListener implements ActionListener {
		private int alphaVal;
		private boolean fadeout, fadein;

		@Override
		public void actionPerformed(ActionEvent e) {
			if (cycles == 0){
				alphaVal = 255;
				fadeout = true;
				fadein = false;
			}
			cycles++;

			if (cycles > 20) {
				if (fadeout) {
					alphaVal = alphaVal - 10;
					if (alphaVal < 0)
						alphaVal = 0;
					Color tc = message.getForeground();
					message.setForeground(new Color(tc.getRed(), tc.getBlue(), tc.getGreen(), alphaVal));
					if (alphaVal == 0) {
						fadeout = false;
						fadein = true;
						message.setText("Welcome to the Doctor's Office!");
					}
				}
				if (fadein) {
					alphaVal += 10;
					if (alphaVal > 255)
						alphaVal = 255;
					message.setForeground(new Color(0, 0, 0, alphaVal));
					if (alphaVal == 255) {
						fadein = false;
						timer.stop();
					}
				}
			}
		}

	}

}
